<%
'***************************************************
'赤金CMS www.cjAsp.com All Rights Reserved
'文件名称：function.inc
'作用：后台公共方法
'***************************************************
dim admin
set admin = new adminModel

class adminModel
	'文件列表
	public function fileList(dir)
		dim folder, file, fileType, fn, i, tally, begin, curPage
		dim total, temp
		set folder = fso.getFolder(server.mapPath(dir))
		total = folder.files.count
		redim temp(total-1)
		if total > 0 then
			begin = db.getPagePosition(total, page, rows)
			curPage = page * rows
			tally = 1 '计数器
			i = 0
			for each file in folder.files
				if tally >= begin then
					redim preserve temp(i)
					fn = app.hec(file.name)
					fileType = lcase(mid(file.name, inStrRev(file.name, ".") + 1))
					temp(i) = array(fn, file.DateLastModified, app.toSize(file.size), fileType)
					if tally = curPage then
						exit for
					end if
					i = i + 1
				end if
				tally = tally + 1 '计总数
			next
		end if
		set folder = nothing
		fileList = array(total, temp)
	end function

	'下载文件
	public function download(fn)
		on error resume next
		dim name, stream, fsof, size
		if trim(fn) = "" then
			message "没有文件下载"
		end if
		fn = server.mapPath(fn)
		if app.isFile(fn) = false then
			message "文件不存在"
		end if
		response.buffer = true
		response.clear
		set stream = server.createObject("adodb.stream")
		stream.open
		stream.type = 1

		set fsof = fso.getFile(fn)
		size = fsof.size
		name = fsof.name
		set fsof = nothing
		name = replace(name, ".back", "")
		name = replace(name, " ", "_")
		stream.loadFromFile(fn)
		response.addHeader "content-disposition", "attachment; filename=" & name
		response.addHeader "content-length", size
		response.charSet = "gb2312"
		response.contentType = "application/octet-stream"
		response.binaryWrite stream.read
		response.flush
		stream.close
		set stream = nothing
		app.errorLog
	end function

    '更新所有数据
    public function updateWebAllData()
        call app.appCacheDeleteAll()
        call updateCache("all")
        call app.fileCacheDelete()
        call app.createView(0)
    end function

    public function filter(ByVal str)
        dim arrChar, nChar, i
        str = trim(str)
        arrChar = "+,',%,^,&,?,<,>,[,],\,;,:," & chr(34) & "," & chr(0) & ",--"
        nChar = split(arrChar, ",")
        for i = 0 to ubound(nChar)
            str = replace(str, nChar(i), "")
        next
        str = replace(str, "@@", "@")
        filter = str
    end function

    '判断复选框是否被选中
    public function isCheck(val)
        if trim(val) <> "" then isCheck = true else isCheck = false
    end function

    public function message(msg)
        if request("ajax") <> "" then
            app.json msg, 1
        end if
        messageContent = msg
        %><!--#include file="../view/message.html"--><%
        call db.close()
        response.end()
    end function

    public function back(msg, text, url)
        if request("ajax") <> "" then
            app.json msg, 1
        end if
        messageContent = msg
        messageUrl = url
        messageText = text
        %><!--#include file="../view/message.html"--><%
        call db.close()
        response.end()
    end function

    public function formatPrice(ByVal str)
        str = trim(str)
        if len("" & str) < 1 then
            formatPrice = 0
            exit function
        end if
        if not(isNumeric(str)) then str = app.int(str)
        formatPrice = str
    end function

    public function deUploadDir(ByVal str)
        if trim(str) <> "" then str = replace(str, "{$uploadDir}", uploadDir)
        deUploadDir = str
    end function

    public function enUploadDir(ByVal str)
        if trim(str) <> "" then
            str = replace(str, uploadDir, "{$uploadDir}")
        end if
        enUploadDir = str
    end function

    '把ID参数进行格式化，返回以英文逗号分割的字符串
    public function joinID(ByVal str)
        if len(trim(str)) < 1 or isNull(str) then
            joinID = "0"
            exit function
        end if
        dim i, j, k, n(), val, x
        val = "0"
        j = split(str, ",")
        x = 0
        for i = 0 to ubound(j)
            k = trim(j(i))
            if isNumeric(k) and k > 0 then
                x = x + 1
                redim preserve n(i)
                n(i) = k
            end if
        next
        if x > 0 then
            val = join(n, ",")
        end if
        joinID = val
    end function

    '在数组中查找是否包含该数据
    public function inArray(ByVal val, ByVal arr)
        dim i, j
        inArray = false
        val = trim(val)
        j = split(arr, ",")
        if ubound(j) = 0 then
            if val = trim(arr) then inArray = true
            exit function
        end if
        for each i in j
            if trim(i) = val then
                inArray = true
                exit for
            end if
        next
    end function

    '查询数据 SheetName 表名，FieldName 字段，返回false或第1个字段值
    public function queryTable(FieldName, SheetName)
        dim rs
        set rs = db.exec("select " & FieldName & " from cj_" & SheetName, 1)
        if rs.eof then queryTable = false else queryTable = rs(0)
        set rs = nothing
    end function

end class
%>